package com.discuss.community.service.impl;

import com.discuss.community.entity.DiscussPost;
import com.discuss.community.mapper.DiscussMapper;
import com.discuss.community.service.DiscussService;
import com.discuss.community.util.SensitiveFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
@Slf4j
public class DiscussServiceImpl implements DiscussService {

    @Autowired
    DiscussMapper discussMapper;

    @Autowired
    SensitiveFilter sensitiveFilter;

    @Override
    public List<DiscussPost> getDiscussPosts(int userId, int offset, int limit) {
        return discussMapper.selectDiscussPosts(userId, offset, limit);
    }

    @Override
    public int getDiscussRows(int userId) {
        return discussMapper.selectDiscussPostRows(userId);
    }

    @Override
    public int addDiscussPost(DiscussPost discussPost) {
        if(discussPost == null) {
            throw new IllegalArgumentException("参数不能为空");
        }

        // 转义HTML标记
        discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));

        // 过滤敏感数据
        discussPost.setTitle(sensitiveFilter.filter(discussPost.getTitle()));
        discussPost.setContent(sensitiveFilter.filter(discussPost.getContent()));


        return discussMapper.insertDiscussPost(discussPost);
    }

    @Override
    public DiscussPost selectDiscussPostById(int id) {
        return discussMapper.selectDiscussPostById(id);
    }


}
